﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iLangl.Model;
using System.Data;

namespace iLangl.LanguageWire.DAL.iLanglDAL.XMLStorage
{
	public class TranslationDAL: BaseStorageDAL, ITranslationDAL
	{
		#region ITranslationDAL Members

		public Translation ReadTranslation(string translationId)
		{
			dtoXMLStorage.TranslationRow translationRow = Storage.Translation.FindById(translationId);

			if (translationRow == null)
				return null;

			Translation translation = new Translation();
			translation.Id = translationRow.Id;
			translation.Progress = translationRow.Progress;
			translation.Status = translationRow.Status;

			if (!translationRow.IsProjectIdNull())
			{
				translation.Project = new Project();
				translation.Project.Id = translationRow.ProjectId;
			}

			translation.Responsible = DALFacade.Instance.ContactDAL.ReadContact(translationRow.ResponsibleId);

			if (!translationRow.IsCommentNull())
				translation.Comment = translationRow.Comment;


			if (!translationRow.IsTPIdentityNull())
				translationRow.TPIdentity = translationRow.TPIdentity;


			LanguagePair lp = new LanguagePair();
			lp.SourceLanguage = new Language();
			lp.SourceLanguage.LanguageId = translationRow.SourceLanguageCode;
			lp.TargetLanguage = new Language();
			lp.TargetLanguage.LanguageId = translationRow.TargetLanguageCode;
			translation.LanguagePair = lp;

			return translation;
		}

		public Translation ReadTranslationByTPIdentity(string TPIdentity)
		{
			foreach (dtoXMLStorage.TranslationRow tr in Storage.Translation)
			{
				if (String.Compare(tr.TPIdentity.Trim(), TPIdentity.Trim(), true) == 0)
				{
					return ReadTranslation(tr.Id);
				}
			}

			return null;
		}

		public void CreateTranslation(Model.Translation translation, IDbTransaction transaction)
		{
			if (translation == null)
				return;

			dtoXMLStorage.TranslationRow translationRow = Storage.Translation.FindById(translation.Id);

			bool isNew = (translationRow == null);

			if (isNew)
			{
				translationRow = Storage.Translation.NewTranslationRow();
			}

			translationRow.Id = translation.Id;
			translationRow.Progress = translation.Progress;
			translationRow.Comment = translation.Comment;
			translationRow.Status = translation.Status;

			if (translation.Responsible != null)
				translationRow.ResponsibleId = translation.Responsible.Id;

			if (translation.Responsible != null)
			{
				if (translation.Responsible.Company != null)
					DALFacade.Instance.CompanyDAL.CreateCompany((Company)translation.Responsible.Company, transaction);

				DALFacade.Instance.ContactDAL.CreateContact(translation.Responsible, transaction);
			}

			if (!String.IsNullOrEmpty(translation.TPIdentity))
				translationRow.TPIdentity = translation.TPIdentity;

			if (translation.Project != null)
				translationRow.ProjectId = translation.Project.Id;

			if (translation.LanguagePair != null)
			{
				if (translation.LanguagePair.SourceLanguage != null)
					translationRow.SourceLanguageCode = translation.LanguagePair.SourceLanguage.LanguageId;

				if (translation.LanguagePair.TargetLanguage != null)
					translationRow.TargetLanguageCode = translation.LanguagePair.TargetLanguage.LanguageId;
			}

			if (translation.SourceContent != null)
			{
				translationRow.SourceContentId = translation.SourceContent.Id;
			}

			if (translation.TargetContent != null)
			{
				translationRow.TargetContentId = translation.TargetContent.Id;
			}

			if (isNew)
			{
				Storage.Translation.AddTranslationRow(translationRow);
				translationRow.TimeSpan = CurrentTimeSpan;
				UpdateDeliveryTime(eEntityType.Translation, translation.Id, translationRow.TimeSpan);
			}

			if (transaction == null)
			{
				AcceptChanges();
			}
		}

		public IList<Translation> ReadTranslationsByProject(Project project)
		{
			if (project == null)
				return null;

			List<Translation> result = new List<Translation>();

			foreach (dtoXMLStorage.TranslationRow translationRow in Storage.Translation)
			{
				if (string.Compare(translationRow.ProjectId, project.Id, true) == 0)
				{
					Translation translation = new Translation();
					translation.Id = translationRow.Id;
					translation.Progress = translationRow.Progress;


					if (!translationRow.IsResponsibleIdNull())
						translation.Responsible = DALFacade.Instance.ContactDAL.ReadContact(translationRow.ResponsibleId);

					if (!translationRow.IsCommentNull())
						translation.Comment = translationRow.Comment;


					if (!translationRow.IsTPIdentityNull())
						translationRow.TPIdentity = translationRow.TPIdentity;

					translation.Project = project;
					LanguagePair lp = new LanguagePair();

					lp.SourceLanguage = new Language();
					lp.SourceLanguage.LanguageId = translationRow.SourceLanguageCode;

					lp.TargetLanguage = new Language();
					lp.TargetLanguage.LanguageId = translationRow.TargetLanguageCode;

					translation.LanguagePair = lp;

					if (!translationRow.IsSourceContentIdNull())
						translation.SourceContent = project.FindContent(translationRow.SourceContentId);
					if (!translationRow.IsTargetContentIdNull())
						translation.TargetContent = project.FindContent(translationRow.TargetContentId);

					result.Add(translation);
				}
			}

			return result;
		}




		public void UpdateTranslation(Translation translation, IDbTransaction transaction)
		{
			if (translation == null)
				return;

			dtoXMLStorage.TranslationRow tr = Storage.Translation.FindById(translation.Id);

			if (tr == null)
			{
				throw new ApplicationException(String.Format("Translation with Id='{0}' is not found!", translation.Id));
			}

			bool ischanged = false;

			if (translation.Responsible != null)
			{
				if (tr.IsResponsibleIdNull()||String.Compare(tr.ResponsibleId, translation.Responsible.Id, true) != 0)
				{
					Contact responsiblePerson = DALFacade.Instance.ContactDAL.ReadContact(translation.Responsible.TPIdentity);

					if (responsiblePerson == null)
					{
						responsiblePerson = translation.Responsible;
						responsiblePerson.Id = Guid.NewGuid().ToString();
						DALFacade.Instance.ContactDAL.CreateContact(responsiblePerson, null);
					}

					tr.ResponsibleId = responsiblePerson.Id;
					ischanged = true;
				}
			}

			if (translation.TargetContent != null)
			{
				if ((tr.IsTargetContentIdNull())|| String.Compare(tr.TargetContentId, translation.TargetContent.Id, true) != 0)
				{
					tr.TargetContentId = translation.TargetContent.Id;
					ischanged = true;
				}
			}
		
			if ((tr.IsStatusNull())||String.Compare(tr.Status, translation.Status, true) != 0)
			{
				tr.Status = translation.Status;
				ischanged = true;
			}

			if ((tr.IsTPIdentityNull())||String.Compare(tr.TPIdentity, translation.TPIdentity, true) != 0)
			{
				tr.TPIdentity = translation.TPIdentity;
				ischanged = true;
			}

			if ((tr.IsProgressNull())||(tr.Progress != translation.Progress))
			{
				tr.Progress = translation.Progress;
				ischanged = true;
			}


			if (ischanged)
			{
				tr.TimeSpan = CurrentTimeSpan;

				if (transaction == null)
					AcceptChanges();
			}

		}


		#endregion
	}
}
